import pymysql

from conn_mysql.conn_mysql import ConnectMysql

class ApiRegister(ConnectMysql):

    def need_his_id(self):

        sql = 'select id from need_his'

        result_info = self.select_all(sql)

        return result_info

    def company_id(self):
    
        sql = 'select id from company'

        result_info = self.select_all(sql)

        return result_info

    def user_paid(self):
        
        sql = 'select company_id,need_his_id from user_paid'

        result_info = self.select_all(sql)

        return result_info

    def insert_user_paid(self, company_id, need_his_id, pay_status):

        sql = 'insert into user_paid(company_id, need_his_id, pay_status)values(%s, %s, %s)'

        result_info = self.insert_info(sql,(company_id, need_his_id, pay_status))

        return result_info

    def register(self, methods, company_id, need_his_id, pay_status):

        if not methods == 'POST':
            
                return {
                    'status': -1,
                    'errorCode': 4,
                    'errorMsg':f'请检查request.method:{methods}请重新请求',
                    'resultBody':None
                }

        else:

            if all((company_id, need_his_id, pay_status)):

                if not {'id':int(company_id)} in self.company_id():

                    return {
                        'status': -1,
                        'errorCode': 3,
                        'errorMsg':f'请检查参数是否存在,company_id:{company_id}请重新输入',
                        'resultBody':None
                    }

                if not {'id':int(need_his_id)} in self.need_his_id():

                    return {
                        'status': -1,
                        'errorCode': 2,
                        'errorMsg':f'请检查参数是否存在,need_his_id:{need_his_id}请重新输入',
                        'resultBody':None
                    }

                if {'company_id':int(company_id),'need_his_id':int(need_his_id)} in self.user_paid():

                    print(self.user_paid())

                    return {
                            'status': -1,
                            'errorCode': 1,
                            'errorMsg':f'请检查参数是否已提交过,company_id:{company_id},need_his_id:{need_his_id}请重新输入',
                            'resultBody':None
                        }

                if not pay_status in ('1','0'):

                    return {
                        'status': -1,
                        'errorCode': 5,
                        'errorMsg':f'请检查参数是否合格,pay_status:{pay_status}请重新输入',
                        'resultBody':None
                    }

                insert_userpaid_work = self.insert_user_paid(company_id, need_his_id, pay_status)

                if insert_userpaid_work:

                    return {
                            'status': 1,
                            'errorCode': None,
                            'errorMsg':None,
                            'resultBody':str(insert_userpaid_work)
                        }

                else:

                    return {

                            'status': -1,
                            'errorCode': 7,
                            'errorMsg':'请检查提交的数据是否有误',
                            'resultBody': None,
                        } 

            else:

                return {

                    'status': -1,
                    'errorCode': 6,
                    'errorMsg':'请检查提交的数据是否有空值',
                    'resultBody': None,
                } 
